Public Class frmPrimaryKeySelection

  Private mIntTableId As Integer = 0

  Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
    Dim lArrayList As New ArrayList
    Dim lIntCounter As Integer
    Dim lObjClsAddNewTable As New clsAddNewTable

    lArrayList = lObjClsAddNewTable.mFnPopulatePrimaryKeySelectionColumn _
                                      (UCase(cboTableNames.Text), mIntTableId)

    If lArrayList.Count <> 0 Then
      For lIntCounter = 0 To lArrayList.Count - 1
        If lstAvailableColumns.Items.Contains(lArrayList.Item(lIntCounter)) = False Then
          lstAvailableColumns.Items.Add(lArrayList.Item(lIntCounter))
        End If
      Next
    End If

  End Sub

  Private Sub CmdSelectColumn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSelectColumn.Click
    Dim lIntSelectedIndex As Integer
    Dim lIntAvailableColumnsListBoxCount As Integer

    lIntSelectedIndex = lstAvailableColumns.SelectedIndex
    lIntAvailableColumnsListBoxCount = lstAvailableColumns.Items.Count

    If lIntSelectedIndex <> -1 Then
      lstSelectedColumns.Items.Add(lstAvailableColumns.SelectedItem)
      lstAvailableColumns.Items.Remove(lstAvailableColumns.SelectedItem)
      If lIntSelectedIndex < lIntAvailableColumnsListBoxCount - 1 Then
        lstAvailableColumns.SetSelected(lIntSelectedIndex, True)
      ElseIf lIntSelectedIndex = lIntAvailableColumnsListBoxCount - 1 And _
                lIntSelectedIndex <> 0 Then
        lstAvailableColumns.SetSelected(lIntSelectedIndex - 1, True)
      End If
    End If
  End Sub

  Private Sub CmdDeSelectColumn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDeSelectColumn.Click
    Dim lIntSelectedIndex As Integer
    Dim lIntSelectedColumnsListBoxCount As Integer

    lIntSelectedIndex = lstSelectedColumns.SelectedIndex
    lIntSelectedColumnsListBoxCount = lstSelectedColumns.Items.Count

    If lIntSelectedIndex <> -1 Then
      lstAvailableColumns.Items.Add(lstSelectedColumns.SelectedItem)
      lstSelectedColumns.Items.Remove(lstSelectedColumns.SelectedItem)
      If lIntSelectedIndex < lIntSelectedColumnsListBoxCount - 1 Then
        lstSelectedColumns.SetSelected(lIntSelectedIndex, True)
      ElseIf lIntSelectedIndex = lIntSelectedColumnsListBoxCount - 1 And _
              lIntSelectedIndex <> 0 Then
        lstSelectedColumns.SetSelected(lIntSelectedIndex - 1, True)
      End If
    End If
  End Sub

  Private Sub cmdMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMoveUp.Click
    Dim lIntSelectedItemIndex As Integer
    Dim lObjSelectedItem As Object

    lIntSelectedItemIndex = lstSelectedColumns.SelectedIndex
    If lIntSelectedItemIndex >= 1 Then
      lObjSelectedItem = lstSelectedColumns.SelectedItem
      lstSelectedColumns.Items.RemoveAt(lIntSelectedItemIndex)
      lstSelectedColumns.Items.Insert(lIntSelectedItemIndex - 1, lObjSelectedItem)
      lstSelectedColumns.SetSelected(lIntSelectedItemIndex - 1, True)
    End If
    
  End Sub

  Private Sub CmdMoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdMoveDown.Click
    Dim lIntSelectedListBoxItemsCount As Integer
    Dim lIntSelectedItemIndex As Integer
    Dim lObjSelectedItem As Object

    lIntSelectedListBoxItemsCount = lstSelectedColumns.Items.Count
    lIntSelectedItemIndex = lstSelectedColumns.SelectedIndex

    If (lIntSelectedItemIndex < lIntSelectedListBoxItemsCount - 1) _
          And lIntSelectedItemIndex <> -1 Then

      lObjSelectedItem = lstSelectedColumns.SelectedItem
      lstSelectedColumns.Items.RemoveAt(lIntSelectedItemIndex)
      lstSelectedColumns.Items.Insert(lIntSelectedItemIndex + 1, lObjSelectedItem)
      lstSelectedColumns.SetSelected(lIntSelectedItemIndex + 1, True)
    End If

  End Sub

  Private Sub lstAvailableColumns_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstAvailableColumns.GotFocus
    Dim lIntAvailableColumnsCount As Integer

    lIntAvailableColumnsCount = lstAvailableColumns.Items.Count
    If lIntAvailableColumnsCount > 0 Then
      lstAvailableColumns.SetSelected(0, True)
    End If
  End Sub

  Private Sub lstSelectedColumns_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstSelectedColumns.GotFocus
    Dim lIntSelectedColumnCount As Integer
    lIntSelectedColumnCount = lstSelectedColumns.Items.Count

    If lIntSelectedColumnCount > 0 Then
      lstSelectedColumns.SetSelected(0, True)
    End If
  End Sub

  Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
    Dim lObjClsAddNewTable As New clsAddNewTable

    lObjClsAddNewTable.mPrAddPrimaryKeyDetails(lstSelectedColumns, mIntTableId, cboKeyType.Text)
    Me.Close()
  End Sub

  Private Sub frmPrimaryKeySelection_Load(ByVal sender As System.Object _
                                        , ByVal e As System.EventArgs) Handles MyBase.Load

    Dim lObjTableList As New clsAddNewTable
    Dim lTableArrayList As New ArrayList

    lTableArrayList = lObjTableList.mFnTablesList()

    'For lIntCnt As Integer = 0 To lTableArrayList.Count - 1
    '  cboTableNames.Items.Add(lTableArrayList(lIntCnt))
    'Next
    cboTableNames.DataSource = lTableArrayList

    cboTableNames.DisplayMember = "mPrpDisplayMember"
    cboTableNames.ValueMember = "mPrpValueMember"
  End Sub
End Class